package com.qn.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qn.entity.Companydata;
import com.qn.service.CompanyDataService;
import com.qn.utils.Msg;
import com.qn.utils.MyPageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

/**
 * (Companydata)表控制层
 *
 * @author ph
 * @since 2021-05-06 19:28:54
 */
@RestController
@RequestMapping("companydata")
public class CompanydataController {
    /**
     * 服务对象
     */
    @Resource
    private CompanyDataService companydataService;

    /**
     * @param myPageInfo 查询条件
     * @return 返回对象列表
     */
    @GetMapping("findByPCIdLimit")
    public Msg findAll(MyPageInfo myPageInfo) {
        PageHelper.startPage(myPageInfo.getOffset(), myPageInfo.getLimit());
        PageInfo<Companydata> companyDataPageInfo = new PageInfo<>(this.companydataService.findByPCIdLimit(myPageInfo));
        return Msg.success().add("findByPCIdLimit", companyDataPageInfo);
    }

    @GetMapping("findAllAvailableEmail")
    public Msg findAllAvailableEmail(MyPageInfo myPageInfo) {
        PageHelper.startPage(myPageInfo.getOffset(), myPageInfo.getLimit());
        PageInfo<String> companyDataPageInfo = new PageInfo<>(this.companydataService.findAllEmail(myPageInfo));
        return Msg.success().add("availableEmail", companyDataPageInfo);
    }

    /**
     * @param integers 数组ID
     * @return 返回数组对应的email
     */
    @GetMapping("findByIdsEmail")
    public Msg findByIdsEmail(Integer[] integers) {
        List<String> stringList = new LinkedList<>();
        for (Integer integer : integers) {
            String email = this.companydataService.findByIdsEmail(integer);
            stringList.add(email);
        }
        return Msg.success().add("200", stringList);
    }

    /**
     * 删除不需要的客户信息
     */
    @DeleteMapping("deleteThisInfo/{id}")
    public Msg deleteThisInfo(@PathVariable Integer id) {
        return Msg.success().add("returnBool", this.companydataService.deleteThisInfo(id) == 1);
    }

    /**
     * 数据去重
     *
     * @return java.lang.Integer
     * @description TODO
     * @author LinXiPeng
     * @date 2021/11/3
     */
    @DeleteMapping("filterRepeat")
    public Integer filterRepeat() {
        //数据的去重
        return this.companydataService.filterRepeat();
    }
}
